<?php


namespace App\Http\Repositories\UserCenter;


use App\Exceptions\ApiException;
use App\Http\Repositories\BaseRepository as Repository;
use App\Models\UserModels\Models\DepartmentModel;

class DepartmentRepository extends Repository
{
    public $department;
    public function __construct(DepartmentModel $department)
    {
        $this->department = $department;
    }

    public function getDepartmentInfo($data)
    {
        return $this->department->getInfo($data);
    }

    public function getDepartmentList($data)
    {
        if ($data['operate'] == 1) {
            is_real_exists(@$data['department']) && $where['department'] = ['like', '%'.$data['department'].'%'];

            return $this->department->getListByPage($where ?? []);
        }

        $department = $this->department->getList();
        return $this->generateTree($department->toArray());
    }

    public function addDepartment($data)
    {
        $data = $this->department->setUserInfo($data);
        $insert = $this->department->create($data);

        if ($data['parent_id'] != 0) {
            $ids = $this->department
                ->where('id', $data['parent_id'])
                ->first();
            $children_id_str = $ids->children_id_str.','.$insert->id;
            $ids->children_id_str = $children_id_str;

            $ids->save();
        }

        return $insert;

    }

    public function delDepartment($data)
    {
        $id = explode(',', $data['ids']);
        $success = 0;
        $fail = 0;
        $message = '';
        foreach ($id as $k => $v) {
            $cur = $this->department->find($v);
            if (empty($cur)) {
                $fail ++;
                $message .= '第'.($k+1).'个删除失败的原因:数据不存在;';
            } else {
                $success ++;
                //更新父级 children_id_str
                $this->updateParentData($cur);
                $cur->delete();
            }
        }

        return ['message' => '成功删除'.$success.'个;失败'.$fail.'个;'.$message];
    }

    public function editDepartment($data)
    {
        $cur = $this->department->find($data['id']);
        if (is_null($cur)) {
            throw new ApiException(ApiException::ERROR_DATA_NOT_EXISTS);
        }
        //更新父级 children_id_str
        $this->updateParentData($cur);
        //更新
        $cur->update($data);
        //更新子级
        $parent_id = $this->department->where('parent_id', $cur->parent_id)->pluck('id')->toArray();
        $parent_id = implode(',', $parent_id);
        $this->department->where('id', $cur->parent_id)->update(['children_id_str' => $parent_id]);

        return [];
    }

    public function updateParentData($cur)
    {
        if ($cur->parent_id != 0) {
            $old = $this->department->where('id', $cur->parent_id)->first();
            if (!is_null($old)) {
                $parent_id = $this->department->where('parent_id', $cur->parent_id)->pluck('id')->toArray();
                $key = array_search($cur->id, $parent_id);
                unset($parent_id[$key]);
                $parent_id = implode(',', $parent_id);
                $old->update(['children_id_str' => $parent_id]);
            }
        }
    }
}
